package com.example.sup.singleton;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sup.constans.TaskStateEnum;
import com.example.sup.entity.TaskManagement;
import com.example.sup.service.TaskManagementService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;


import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ZbScheduleConfig
 * @Description {TODO}
 * @Author 智邦教育.陈超
 * @Date 10:06 上午 2024/8/1
 * @Version 1.0
 */
@Configuration
public class ZbScheduleConfig  implements SchedulingConfigurer {
    @Resource
    TaskManagementService taskManagementService;
    /**
     * 当前SchedulingConfigurer类实例化后，自动调用下面这个方法，执行定时任务
     * @param taskRegistrar 定时任务注册器
     *
     *----炸弹(任务)-----引线(触发器)------点火人(调度器)
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("定时任务执行了");
            }
        };
        //查询数据库taskmanagement表，获取所有定时任务的cron表达式

        QueryWrapper<TaskManagement> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(TaskManagement::getState, TaskStateEnum.ON.getCode());
        List<TaskManagement> taskManagementList = taskManagementService.list(queryWrapper);
        taskManagementList.forEach(taskManagement -> {
            taskRegistrar.addCronTask(runnable,taskManagement.getCron());
        });
//        taskRegistrar.addCronTask(runnable,"0/2 * * ? * *");
    }
}
